package com.niit.mall.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.mall.common.domain.dto.PageDTO;
import com.niit.mall.common.utils.BeanUtils;
import com.niit.mall.order.domain.dto.*;
import com.niit.mall.order.domain.po.Order;
import com.niit.mall.order.domain.po.OrderItem;
import com.niit.mall.order.domain.po.OrderReturnApply;
import com.niit.mall.order.domain.po.OrderReturnReason;
import com.niit.mall.order.domain.query.ReturnApplyQuery;
import com.niit.mall.order.mapper.OrderItemMapper;
import com.niit.mall.order.mapper.OrderMapper;
import com.niit.mall.order.mapper.OrderReturnApplyMapper;
import com.niit.mall.order.mapper.OrderReturnReasonMapper;
import com.niit.mall.order.service.IOrderReturnApplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.order.service.impl
 * @Author: XihenXi
 * @CreateTime: 2025-06-18  15:06
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyMapper, OrderReturnApply> implements IOrderReturnApplyService {

    private final OrderMapper orderMapper;
    private final OrderItemMapper orderItemMapper;
    private final OrderReturnReasonMapper orderReturnReasonMapper;

    /**
     * 分页查询所有退单
     * @param query 查询参数
     * @return 分页退单视图对象
     */
    @Override
    public PageDTO<OrderReturnApplyDTO> queryAllRefund(ReturnApplyQuery query) {
        //获取查询参数
        Long id = query.getId();
        Integer status = query.getStatus();
        String keyword = query.getKeyword();
        String handleMan = query.getHandleMan();
        LocalDate createDateStart = query.getCreateDateStart();
        LocalDate createDateEnd = query.getCreateDateEnd();
        LocalDateTime handleTimeStart = query.getHandleTimeStart();
        LocalDateTime handleTimeEnd = query.getHandleTimeEnd();
        String sortBy = query.getSortBy();
        Boolean isAsc = query.getIsAsc();

        // 分页条件
        Page<OrderReturnApply> page = query.toMpPage(sortBy, isAsc);

        // 执行查询
        Page<OrderReturnApply> returnApplyList = lambdaQuery()
                .eq(id != null, OrderReturnApply::getId, id)
                .eq(status != null, OrderReturnApply::getStatus, status)
                .like(keyword != null, OrderReturnApply::getReturnName, keyword)
                .like(keyword != null, OrderReturnApply::getReturnPhone, keyword)
                .like(handleMan != null, OrderReturnApply::getHandleMan, handleMan)
                .ge(createDateStart != null, OrderReturnApply::getCreateDate, createDateStart)
                .le(createDateEnd != null, OrderReturnApply::getCreateDate, createDateEnd)
                .ge(handleTimeStart != null, OrderReturnApply::getHandleTime, handleTimeStart)
                .le(handleTimeEnd != null, OrderReturnApply::getHandleTime, handleTimeEnd)
                .page(page);

        return PageDTO.of(returnApplyList, OrderReturnApplyDTO.class);
    }

    /**
     * 根据退单id获取退单详情
     * @param id 退单id
     * @return 退单详情
     */
    @Override
    public OrderReturnApplyDetailDTO getDetailById(Long id) {
        // 获取退单
        OrderReturnApply orderReturnApply = getById(id);
        OrderReturnApplyDTO orderReturnApplyDTO = BeanUtils.copyBean(orderReturnApply, OrderReturnApplyDTO.class);

        // 获得对应的原因id
        Integer reasonId = orderReturnApply.getReasonId();

        // 获取原因
        OrderReturnReason orderReturnReason = orderReturnReasonMapper.selectOne(new QueryWrapper<OrderReturnReason>().eq("id", reasonId));
        OrderReturnReasonDTO orderReturnReasonDTO = BeanUtils.copyBean(orderReturnReason, OrderReturnReasonDTO.class);

        // 获取对应的订单id
        Long orderId = orderReturnApply.getOrderId();

        // 获取订单
        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("id", orderId));
        OrderDTO orderDTO = BeanUtils.copyBean(order, OrderDTO.class);

        // 获取订单项
        List<OrderItem> orderItemList = orderItemMapper.selectList(new QueryWrapper<OrderItem>().eq("order_id", orderId));
        List<OrderItemDTO> orderItemDTOList = BeanUtils.copyList(orderItemList, OrderItemDTO.class);

        return new OrderReturnApplyDetailDTO(orderReturnApplyDTO, orderReturnReasonDTO, orderDTO, orderItemDTOList);
    }
}
